package pl.us.ij.plugins.segmentation.forms;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import pl.us.ij.plugins.segmentation.commands.threshold.GradientThresholdCommand;
import pl.us.ij.plugins.segmentation.commands.threshold.HistogramThresholdCommand;
import pl.us.ij.plugins.segmentation.commands.threshold.IterativeHistogramThresholdCommand;
import pl.us.ij.plugins.segmentation.infrastructure.CategoryInfo;
import pl.us.ij.plugins.segmentation.localization.Localization;

import javax.swing.*;
import java.util.ResourceBundle;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-05-25
 * Time: 21:12:14
 * To change this template use File | Settings | File Templates.
 */
public class ThresholdsAutoForm extends CategoryInfo {
    private JPanel panel1;
    private JButton histogramButton;
    private JButton iterativeHistogramButton;
    private JButton gradientButton;

    public ThresholdsAutoForm(IActionHandler actionHandler) {
        actionHandler.setAction(histogramButton, HistogramThresholdCommand.class, null);
        actionHandler.setAction(iterativeHistogramButton, IterativeHistogramThresholdCommand.class, null);
        actionHandler.setAction(gradientButton, GradientThresholdCommand.class, null);
    }

    @Override
    public String getTitle() {
        return Localization.getString("thresholdsAutoForm.title");
    }

    @Override
    public JPanel getPanel() {
        return panel1;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String getDescription() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:d:grow", "center:max(d;4px):noGrow"));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new FormLayout("fill:d:noGrow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        CellConstraints cc = new CellConstraints();
        panel1.add(panel2, cc.xy(1, 1, CellConstraints.LEFT, CellConstraints.DEFAULT));
        gradientButton = new JButton();
        this.$$$loadButtonText$$$(gradientButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("thresholdsAutoForm.gradientLabel"));
        panel2.add(gradientButton, cc.xy(1, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
        iterativeHistogramButton = new JButton();
        this.$$$loadButtonText$$$(iterativeHistogramButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("thresholdsAutoForm.iterationHistogramLabel"));
        panel2.add(iterativeHistogramButton, cc.xy(1, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
        histogramButton = new JButton();
        this.$$$loadButtonText$$$(histogramButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("thresholdsAutoForm.histogramLabel"));
        panel2.add(histogramButton, cc.xy(1, 5, CellConstraints.FILL, CellConstraints.DEFAULT));
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadButtonText$$$(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return panel1;
    }
}
